package com.example.searchcenter.center;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.searchcenter.R;
import com.example.searchcenter.helper.AlertDialogManager;
import com.example.searchcenter.helper.ConnectionDetector;
import com.example.searchcenter.helper.JSONParser;
import com.example.searchcenter.model.CenterItemModel;

import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ResultSearchCenters extends FragmentActivity {
	ListView list;
	TextView noti_result;
	TextView name;
	TextView thumbnail;
	View rootView;
	//
	ConnectionDetector cd;
	// Input for search
	public String inputSearch;
	// Alert dialog manager
	AlertDialogManager alert = new AlertDialogManager();
	// array list centermodel
	ArrayList<CenterItemModel> centerslist = new ArrayList<CenterItemModel>();
	// URL to get JSON Array
	private static String url = "http://tinhnguyen.edu.vn/apis/placeSearch";
	// JSON Node Names
	public static final String TAG_CENTERS = "places";
	public static final String TAG_ID_CENTER = "cid";
	public static final String TAG_NAME = "name";
	public static final String TAG_THUMBNAIL = "thumbnailImageUrl";
	public static final String TAG_NO = "no";
	public static final String TAG_STREET = "street";
	public static final String TAG_WARD = "ward";
	public static final String TAG_DISTRICT = "district";
	public static final String TAG_CITY = "city";
	public static final String TAG_CREATEDATE = "createDate";
	public static final String TAG_SUM = "sumPlaces";

	// JSON parser class
	JSONParser jsonParser = new JSONParser();

	JSONArray arrayJsonCenter = null;
	CenterAdapter adapter;
	JSONObject jsonobject;
	JSONArray jsonarray;

	public ResultSearchCenters() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_result_search);

		// ActionBar back navigation
		ActionBar actionbar = getActionBar();
		actionbar.setDisplayHomeAsUpEnabled(true);
		actionbar.setHomeButtonEnabled(true);

		// Getting input search from HomeFragment
		Intent in = getIntent();
		inputSearch = in.getStringExtra("inputSearch");
		Log.w("input", inputSearch);

		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("query", inputSearch));

		// getting product details by making HTTP request
		// Note that product details url will use GET request
		JSONObject json = jsonParser.makeHttpRequest(url, "GET", params);

		// check your log for json response
		Log.d("Single Center Details", json.toString());

		try {
			// Getting JSON Array from URL
			Log.w("JSON", json.toString());
			arrayJsonCenter = json.getJSONArray(TAG_CENTERS);
			for (int i = 0; i < arrayJsonCenter.length(); i++) {

				JSONObject c = arrayJsonCenter.getJSONObject(i);
				// Storing JSON item in a Variable
				String cid = c.getString(TAG_ID_CENTER);
				String name = c.getString(TAG_NAME);
				String thumbnail = c.getString(TAG_THUMBNAIL);
				String no = c.getString(TAG_NO);
				String street = c.getString(TAG_STREET);
				String ward = c.getString(TAG_WARD);
				String district = c.getString(TAG_DISTRICT);
				String city = c.getString(TAG_CITY);
				String createDate = c.getString(TAG_CREATEDATE);

				// String address
				String addr = "Số " + no + " ,Đường " + street + " ,Phường/xã "
						+ ward + " ,Quận/huyện " + district + " ,thành phố "
						+ city;

				CenterItemModel centerItem = new CenterItemModel(cid, name,
						addr, createDate, thumbnail);
				centerslist.add(centerItem);

			}

			// get sumPlaces in JSON
			noti_result = (TextView) findViewById(R.id.noti_result);
			noti_result.setText("Tìm kiếm được " + json.getInt("sumPlaces")
					+ " địa điểm");

			list = (ListView) findViewById(R.id.list_result);

			adapter = new CenterAdapter(ResultSearchCenters.this, centerslist);

			list.setAdapter(adapter);

			list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {

					// Starting new intent
					Intent in = new Intent(ResultSearchCenters.this,
							DetailsCenter.class);
					// sending pid to next activity
					in.putExtra(TAG_ID_CENTER, centerslist.get(position)
							.getCenterId());

					// starting new activity and expecting some response
					// back
					startActivity(in);

					Toast.makeText(
							ResultSearchCenters.this,
							"Bạn đã chọn  "
									+ centerslist.get(+position)
											.getCenterName(),
							Toast.LENGTH_SHORT).show();
				}
			});

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
}